package com.itheima.consumer.listeners;

import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class MqListenerTest {

    @RabbitListener(queues = "simple.queue")   //选择监听的队列
    public void listenSimpleQueue(String message) {
        log.info("消费者接收到消息：{}", message);
        throw new RuntimeException("故意抛出一个异常");
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
        log.info("消费者消费者1接收到work.queue消息：{}", message);
        Thread.sleep(20);
    }


    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        log.info("消费者2接收到work.queue消息：{}", message);
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) throws InterruptedException {
        log.info("消费者1接收到fanout.queue1消息：{}", message);
    }


    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) throws InterruptedException {
        log.info("消费者2接收到fanout.queue2消息：{}", message);
    }


    @RabbitListener(bindings = @QueueBinding(   //使用注解创建虚拟机和队列绑定关系
            value=@Queue(name="direct.queue1",durable = "true"),
            exchange=@Exchange(name="hmall.direct",type = ExchangeTypes.DIRECT),
            key={"red","blue"}
    ))
    public void listenDirectQueue1(String message) throws InterruptedException {
        log.info("消费者1接收到direct.queue1消息：{}", message);
    }


    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name="direct.queue2",durable = "true"),
            exchange=@Exchange(name="hmall.direct",type = ExchangeTypes.DIRECT),
            key={"red","yellow"}
    ))
    public void listenDirectQueue2(String message) throws InterruptedException {
        log.info("消费者2接收到direct.queue2消息：{}", message);
    }


    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String message) throws InterruptedException {
        log.info("消费者1接收到topic.queue1消息：{}", message);
    }


    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String message) throws InterruptedException {
        log.info("消费者2接收到topic.queue2消息：{}", message);
    }


    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String, Object>  message) throws InterruptedException {
        log.info("消费者接收到object.queue消息：{}", message);
    }

}
